package com.watch.system.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.watch.system.common.result.PageResult;
import com.watch.system.common.result.Result;
import com.watch.system.common.util.ExcelUtils;
import com.watch.system.model.form.ResourcesForm;
import com.watch.system.model.form.ResourcesImgForm;
import com.watch.system.model.query.CarAuthPageQuery;
import com.watch.system.model.query.CarResoucesPageQuery;
import com.watch.system.model.vo.*;
import com.watch.system.plugin.easyexcel.AuthImportListener;
import com.watch.system.plugin.easyexcel.ResourcesImportListener;
import com.watch.system.service.ResourcesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 资源
 * @author gmli
 * @since 2024/1/29
 */
@Tag(name = "11.资源大厅")
@RestController
@RequestMapping("/api/car/resources")
@RequiredArgsConstructor
public class ResourcesController {

    private final ResourcesService resourcesService;

    @Operation(summary = "资源分页列表")
    @GetMapping("/page")
    public PageResult<CarResourcesPageVO> getResourcesPage(@ParameterObject CarResoucesPageQuery queryParams) {
        IPage<CarResourcesPageVO> result = resourcesService.getResourcesPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "资源分页列表")
    @GetMapping("/pageImg")
    public PageResult<CarResourcesImgPageVO> getResourcesImgPage(@ParameterObject CarResoucesPageQuery queryParams  ) {
        IPage<CarResourcesImgPageVO> result = resourcesService.getResourcesImgPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "添加图片")
    @PutMapping(value = "/saveResourcesImg")
    public Result saveResourcesImg(@RequestBody @Validated ResourcesImgForm resourcesImgForm) {
        boolean result = resourcesService.saveResourcesImg(resourcesImgForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改模板用户")
    @PutMapping(value = "/saveResources")
     public Result saveResources(@RequestBody @Validated ResourcesForm resourcesForm) {
        boolean result = resourcesService.saveResources(resourcesForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{ids}")
    public Result deleteResources(@Parameter(description = "用户ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = resourcesService.deleteResources(ids);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/del/{ids}")
    public Result deleteResourcesImg(@Parameter(description = "用户ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = resourcesService.deleteResourcesImg(ids);
        return Result.judge(result);
    }

    @Operation(summary = "资源导入模板下载")
    @GetMapping("/templateResources")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        String fileName = "资源模板.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

        String fileClassPath = "excel-templates" + File.separator + fileName;
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileClassPath);

        ServletOutputStream outputStream = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();

        excelWriter.finish();
    }

    @Operation(summary = "导入资源")
    @PostMapping("/impResources")
    public Result importResources(MultipartFile file) throws IOException {
        ResourcesImportListener listener = new ResourcesImportListener();
        String msg = ExcelUtils.importExcel(file.getInputStream(), CarResourcesImportVO.class, listener);
        return Result.success(msg);
    }

    @Operation(summary = "导出资源")
    @GetMapping("/expResources")
    public void exportResources(CarResoucesPageQuery queryParams, HttpServletResponse response) throws IOException {
        String fileName = "资源数据.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

        List<CarResourcesExportVO> exportUserList = resourcesService.listExportResources(queryParams);
        EasyExcel.write(response.getOutputStream(), CarResourcesExportVO.class).sheet("资源列表")
                .doWrite(exportUserList);
    }
}
